package com.gbei.center.utils.message;


public class Result<T> {
    // 状态码：0-成功；-1-操作失败；)
    private Integer code = ResponseInfo.SUCCESS.getCode();
    private boolean success = ResponseInfo.SUCCESS.getSuccess();
    private String message = ResponseInfo.SUCCESS.getMessage();
    private Long timestamp = System.currentTimeMillis();
    private Long version;
    private T data;
    
    public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	public Integer getCode() {
        return code;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public Result<T> setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Result() {}
    
    public Result(Integer code, String message) {
        super();
        this.code = code;
        this.timestamp = System.currentTimeMillis();
        this.message = message;
        this.success = true;
    }
    
    public Result(Integer code, String message,Long time) {
        super();
        this.code = code;
        this.version = time;
        this.message = message;
        this.success = true;
    }
    
    public Result(Integer code, String message, Boolean isSuccess) {
        super();
        this.code = code;
        this.timestamp = System.currentTimeMillis();
        this.message = message;
        this.success = isSuccess;
    }

    public Result(Integer code, String message, Boolean isSuccess,Long time) {
        super();
        this.code = code;
        this.version = time;
        this.message = message;
        this.success = isSuccess;
    }
    
    public Result(Integer code, String message, T data) {
        super();
        this.code = code;
        this.timestamp = System.currentTimeMillis();
        this.message = message;
        this.success = true;
        this.data = data;
    }
    
    public Result(Integer code, String message, T data,Long time) {
        super();
        this.code = code;
        this.version = time;
        this.message = message;
        this.success = true;
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" + "code=" + code + ", success=" + success + ", message='" + message + '\'' + ", timestamp="
               + timestamp + ", version=" + version + ", data=" + data + '}';
    }

    /**
     * 构造成功的Result对象
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> buildSuccess(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(ResponseInfo.SUCCESS.getSuccess());
        result.setCode(ResponseInfo.SUCCESS.getCode());
        result.setMessage(ResponseInfo.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 构造Result对象
     *
     * @param responseInfo
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> build(ResponseInfo responseInfo, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(responseInfo.getSuccess());
        result.setCode(responseInfo.getCode());
        result.setMessage(responseInfo.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 构造Result对象
     *
     * @param responseInfo
     * @param <T>
     * @return
     */
    public static <T> Result<T> build(ResponseInfo responseInfo) {
        Result<T> result = new Result<>();
        result.setSuccess(responseInfo.getSuccess());
        result.setCode(responseInfo.getCode());
        result.setMessage(responseInfo.getMessage());
        return result;
    }
}
